package com.sen.pro.switches.admin.common;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sen.common.struts.BaseAction;
import com.sen.common.util.JsonBinder;
import com.sen.common.util.web.Struts2Utils;
import com.sen.pro.switches.core.cfg.ENV;
import com.sen.pro.switches.core.common.Constants;
import com.sen.pro.switches.core.util.SiteFileUtil;

/**
 * 文件上传aciton
 * 
 * @author tianqingsen
 */
@Scope("prototype")
@Controller("fileUploadAction")
@Namespace("/admin/upload")
public class FileUploadAction extends BaseAction {

	private static final long serialVersionUID = -5658167324342534592L;
	private Logger LOG = Logger.getLogger(FileUploadAction.class);

	private File fileInput;
	private String fileInputFileName;
	private String fileInputContentType;
	private String dirType;
	private String type;

	/**
	 * 企业LOGO图片上传处理
	 * @return
	 */
	@Action(value = "logo_process")
	public String logoProcess() {
		try {
			String siteImgDir = ServletActionContext.getServletContext().getRealPath("/") + ENV.IMG_DIR;
			String logoImgName = Constants.LOGO_IMG_NAME + "." + FilenameUtils.getExtension(fileInputFileName);
			String siteImgUrl = ENV.IMG_DIR + "/" + logoImgName;

			// 删除旧LOGO图片
			SiteFileUtil.delSiteLogoImg(siteImgDir);

			// 上传
			String toFilePath = siteImgDir + "/" + logoImgName;
			SiteFileUtil.siteFileUpload(fileInput, toFilePath);
			sendMessage(true, FilenameUtils.separatorsToUnix(new File(siteImgUrl).getPath()));
		} catch (IOException e) {
			LOG.error("LOGO 图片上传失败，原因：" + e.getMessage(), e);
			sendMessage(false, "上传失败，系统正忙！");
		}
		return NONE;
	}

	/**
	 * 宣传图片一上传处理
	 * @return
	 */
	@Action(value = "adv1_process")
	public String adv1Process() {
		try {
			final String advsIdx = "1";
			String retImgUrl = advsProcess(advsIdx);
			sendMessage(true, FilenameUtils.separatorsToUnix(new File(retImgUrl).getPath()));
		} catch (IOException e) {
			LOG.error("宣传图片一上传失败，原因：" + e.getMessage(), e);
			sendMessage(false, "上传失败，系统正忙！");
		}
		return NONE;
	}

	/**
	 * 宣传图片二上传处理
	 * @return
	 */
	@Action(value = "adv2_process")
	public String adv2Process() {
		try {
			final String advsIdx = "2";
			String retImgUrl = advsProcess(advsIdx);
			sendMessage(true, FilenameUtils.separatorsToUnix(new File(retImgUrl).getPath()));
		} catch (IOException e) {
			LOG.error("宣传图片二上传失败，原因：" + e.getMessage(), e);
			sendMessage(false, "上传失败，系统正忙！");
		}
		return NONE;
	}

	/**
	 * 宣传图片三上传处理
	 * @return
	 */
	@Action(value = "adv3_process")
	public String adv3Process() {
		try {
			final String advsIdx = "3";
			String retImgUrl = advsProcess(advsIdx);
			sendMessage(true, FilenameUtils.separatorsToUnix(new File(retImgUrl).getPath()));
		} catch (IOException e) {
			LOG.error("宣传图片三上传失败，原因：" + e.getMessage(), e);
			sendMessage(false, "上传失败，系统正忙！");
		}
		return NONE;
	}

	/**
	 * 宣传图片四上传处理
	 * @return
	 */
	@Action(value = "adv4_process")
	public String adv4Process() {
		try {
			final String advsIdx = "4";
			String retImgUrl = advsProcess(advsIdx);
			sendMessage(true, FilenameUtils.separatorsToUnix(new File(retImgUrl).getPath()));
		} catch (IOException e) {
			LOG.error("宣传图片四上传失败，原因：" + e.getMessage(), e);
			sendMessage(false, "上传失败，系统正忙！");
		}
		return NONE;
	}

	/**
	 * 宣传图片五上传处理
	 * @return
	 */
	@Action(value = "adv5_process")
	public String adv5Process() {
		try {
			final String advsIdx = "5";
			String retImgUrl = advsProcess(advsIdx);
			sendMessage(true, FilenameUtils.separatorsToUnix(new File(retImgUrl).getPath()));
		} catch (IOException e) {
			LOG.error("宣传图片五上传失败，原因：" + e.getMessage(), e);
			sendMessage(false, "上传失败，系统正忙！");
		}
		return NONE;
	}

	/**
	 * 宣传图片上传处理
	 * @param advsIdx
	 * @return 能够访问的图片url
	 * @throws IOException 
	 */
	private String advsProcess(String advsIdx) throws IOException {
		String siteImgDir = ServletActionContext.getServletContext().getRealPath("/") + ENV.IMG_DIR;
		String advsImgName = SiteFileUtil.genAdvsImgName(advsIdx) + "." + FilenameUtils.getExtension(fileInputFileName);
		String siteImgUrl = ENV.IMG_DIR + "/" + advsImgName;

		// 删除指定索引的宣传图片
		SiteFileUtil.delSiteAdvsImg(siteImgDir, advsIdx);

		// 上传
		String toFilePath = siteImgDir + "/" + advsImgName;
		SiteFileUtil.siteFileUpload(fileInput, toFilePath);
		return siteImgUrl;
	}

	/**
	 * 返回json格式的字符串信息到客户端
	 * @param sussFlag
	 * @param message
	 */
	private void sendMessage(boolean sussFlag, String message) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("successful", sussFlag + "");
		map.put("message", message);
		Struts2Utils.renderHtml(JsonBinder.buildNormalBinder().toJson(map));
	}

	public File getFileInput() {
		return fileInput;
	}

	public void setFileInput(File fileInput) {
		this.fileInput = fileInput;
	}

	public String getFileInputFileName() {
		return fileInputFileName;
	}

	public void setFileInputFileName(String fileInputFileName) {
		this.fileInputFileName = fileInputFileName;
	}

	public String getFileInputContentType() {
		return fileInputContentType;
	}

	public void setFileInputContentType(String fileInputContentType) {
		this.fileInputContentType = fileInputContentType;
	}

	public String getDirType() {
		return dirType;
	}

	public void setDirType(String dirType) {
		this.dirType = dirType;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
}
